<?php
/****************************************************
 * $Date:: Jun 8, 2010 12:06:11 AM					*
 * $Revision:										*
 * $Author:: duongthienduc							*
 * $Comment::										*
 ****************************************************/

class AuthController extends Bet_Client_Controller {

    public function init() {
    	parent::init();
        $this->_helper->acl->allow(null);
    }

	public function indexAction() {
		$this->_forward('login');
	}

	public function loginAction() {
		$identity = Zend_Auth::getInstance()->getIdentity();

		$loginForm = new Bet_Form_Auth_Login();
		if ($this->getRequest()->isPost()) {
			if ($loginForm->isValid($_POST)) {
				$db = $this->_getParam('db');
				$dbAdapter = Bet_Db_Table::getDefaultAdapter();
				$tblName = Bet_Util_Db::getTableNameWithPrefix('user', $dbAdapter->getConfig());
				$adapter = new Bet_Auth_Adapter_DbTable(
					$dbAdapter,
					$tblName,
					'username',
					'password',
					'MD5(?)'
				);
				
	
				$adapter->setIdentity($loginForm->getValue('username'));
				$adapter->setCredential($loginForm->getValue('password'));
	
				$result = Zend_Auth::getInstance()->authenticate($adapter);
				if ($result->isValid()) {
					$this->_helper->FlashMessenger('Successful Login');
					$this->_redirect('/admin');
					return;
				}
			}
		}

		$this->view->form = $loginForm;
	}

	public function logoutAction() {
		$identity = Zend_Auth::getInstance()->getIdentity();
		if (!empty($identity)) {
			Zend_Auth::getInstance()->clearIdentity();
		}
		$this->_redirect('/admin/auth/login');
		
	}

	protected function requireLoggedIn($back = null) {
		return false;
	}

}

?>